package com.situ.crm.controller;

import com.situ.crm.pojo.entity.Permission;
import com.situ.crm.pojo.entity.User;
import com.situ.crm.service.IPermissionService;
import com.situ.crm.service.IUserService;
import com.situ.crm.util.JSONResult;
import com.situ.crm.util.LayUITableResult;
import com.situ.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RequestMapping("/user")
@Controller
// @RestController = @Controller + 所有方法@ResponseBody
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IPermissionService permissionService;

	@RequestMapping("/getUserPage")
	public String getUserPage() {
		return "/user/user_list";
	}

	@RequestMapping("/list")
	@ResponseBody
	public LayUITableResult list(Integer page, Integer limit, User user, Date time) {
		System.out.println(time);
		return userService.selectByPage(page, limit, user);
	}

	@RequestMapping("/deleteById")
	@ResponseBody
	public JSONResult deleteById(Integer id) {
		userService.deleteById(id);
		return JSONResult.ok("删除成功");
	}

	@RequestMapping("/deleteAll")
	@ResponseBody
	public JSONResult deleteAll(String ids) {
		userService.deleteAll(ids);
		return  JSONResult.ok("删除成功");
	}

	@RequestMapping("/getInsertPage")
	public String getInsertPage() {
		return "/user/user_insert";
	}

	@PostMapping("/insert")
	@ResponseBody
	public JSONResult insert( User user, BindingResult result, String roles) {
		System.out.println(user);
		System.out.println(roles);

		userService.insert(user, roles);
		return JSONResult.ok("添加成功");
	}

	@RequestMapping("/update")
	@ResponseBody
	public JSONResult update(User user, String roles) {
		System.out.println(user);
		System.out.println(roles);
		userService.update(user, roles);
		return JSONResult.ok("更新成功");
	}

	@RequestMapping("/getUpdatePage")
	public String getUpdatePage() {
		return "/user/user_update";
	}

	// @RequestMapping(value = "/selectUserAndRoles", method = RequestMethod.GET)
	@RequestMapping("/selectUserAndRoles")
	@ResponseBody
	public JSONResult selectUserAndRoles(Integer userId) {
		Map<String, Object> map = userService.selectUserAndRoles(userId);
		return JSONResult.ok(map);
	}

	// 如果不加method = RequestMethod.POST，这个方法默认所有的请求GET/POST/PUT/DELETE都可以处理
	// 如果限定指定处理POST，而请求的是GET请求，就会报下面的错误
	// There was an unexpected error (type=Method Not Allowed, status=405).
	// Request method 'GET' not supported

	// @RequestMapping(value = "/login", method = RequestMethod.POST)
	@PostMapping("/login")
	@ResponseBody
	public JSONResult login(String name, String password, HttpSession session) {
		UserContext.session = session;
		User user = userService.login(name, password, session);
		if (user != null) {
			// 把当前用户放到session中
			session.setAttribute(UserContext.USER_IN_SESSION, user);
			// 查询该用户拥有的权限,并放到session中
			List<Permission> permissions = permissionService.selectByUserId(user.getId());
			session.setAttribute(UserContext.PERMISSION_IN_SESSION, permissions);
			return JSONResult.ok("登录成功");
		}
		return JSONResult.error("登录失败");
	}
	
	@RequestMapping("/getLoginPage")
	public String getLoginPage() {
		return "/user/login";
	}


}


